package com.ruoyi.web.controller.datastatistics;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.basedata.domain.UsersXb;
import com.ruoyi.common.utils.MapKeyUnderlineToCamelUtil;
import com.ruoyi.basedata.domain.vo.UserTerminalsRequestVO;
import com.ruoyi.datastatistics.service.IBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.datastatistics.domain.PublicShareUserJoin;
import com.ruoyi.datastatistics.service.IPublicShareUserJoinService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公摊用户关联Controller
 *
 * @author zt
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/datastatistics/publicShareJoin")
public class PublicShareUserJoinController extends BaseController
{
    @Autowired
    private IPublicShareUserJoinService publicShareUserJoinService;
    @Autowired
    private IBillService billService;

    /**
     * 查询公摊用户关联列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PublicShareUserJoin publicShareUserJoin)
    {
        startPage();
        publicShareUserJoin.setTenantId(getLoginUser().getTenantId());
        List<PublicShareUserJoin> list = publicShareUserJoinService.selectPublicShareUserJoinList(publicShareUserJoin);
        return getDataTable(list);
    }

    /**
     * 查询公摊用户关联列表
     */
    @GetMapping("/getAll")
    public TableDataInfo getAll(PublicShareUserJoin publicShareUserJoin)
    {
        startPage();
        publicShareUserJoin.setTenantId(getLoginUser().getTenantId());
        List<PublicShareUserJoin> list = publicShareUserJoinService.selectListWithUser(publicShareUserJoin);
        return getDataTable(list);
    }

    /**
     * 导出公摊用户关联列表
     */
    @Log(title = "公摊用户关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PublicShareUserJoin publicShareUserJoin)
    {
        List<PublicShareUserJoin> list = publicShareUserJoinService.selectPublicShareUserJoinList(publicShareUserJoin);
        ExcelUtil<PublicShareUserJoin> util = new ExcelUtil<PublicShareUserJoin>(PublicShareUserJoin.class);
        util.exportExcel(response, list, "公摊用户关联数据");
    }

    /**
     * 获取公摊用户关联详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(publicShareUserJoinService.selectPublicShareUserJoinById(id));
    }

    /**
     * 新增公摊用户关联
     */
    @Log(title = "公摊用户关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PublicShareUserJoin publicShareUserJoin)
    {
        publicShareUserJoin.setTenantId(getLoginUser().getTenantId());
        return toAjax(publicShareUserJoinService.insertPublicShareUserJoin(publicShareUserJoin));
    }

    /**
     * 修改公摊用户关联
     */
    @Log(title = "公摊用户关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PublicShareUserJoin publicShareUserJoin)
    {
        return toAjax(publicShareUserJoinService.updatePublicShareUserJoin(publicShareUserJoin));
    }

    /**
     * 删除公摊用户关联
     */
    @Log(title = "公摊用户关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(publicShareUserJoinService.deletePublicShareUserJoinByIds(ids));
    }

    /**
     * 批量新增区域绑定设备
     */
    @Log(title = "挂接设备", businessType = BusinessType.INSERT)
    @PostMapping("/addBindUser")
    public AjaxResult addBindUser(@RequestBody List<PublicShareUserJoin> list) {
        String tenantId = getLoginUser().getTenantId();
        if(!list.isEmpty()){
            list.forEach(publicShareUserJoin -> {
                publicShareUserJoin.setTenantId(tenantId);
                publicShareUserJoin.setUpdateTime(new Date());
            });
            publicShareUserJoinService.saveBatch(list);
        }
        return success("新增成功");
    }

    /**
     * 修改区域绑定设备
     */
    @Log(title = "挂接设备", businessType = BusinessType.UPDATE)
    @PutMapping("/editBindUser")
    public AjaxResult editBindUser(@RequestBody PublicShareUserJoin entity) {
        Long id = entity.getId();
        PublicShareUserJoin oldEntity = publicShareUserJoinService.getById(id);
        if(entity.getPublicShareType()!=null){
            oldEntity.setPublicShareType(entity.getPublicShareType());
        }
        if(entity.getCalcType()!=null){
            oldEntity.setCalcType(entity.getCalcType());
        }
        if(entity.getCalcRate()!=null){
            oldEntity.setCalcRate(entity.getCalcRate());
        }
        publicShareUserJoinService.getBaseMapper().updateById(oldEntity);
        return success("更新成功");
    }

    /**
    * @Description: 查询租户下用户信息-select组件
    * @date: 2025/7/7 10:46
    * @author: zt
    */
    @GetMapping("/getUserListInSelect")
    public AjaxResult getUserListInSelect()
    {
        String tenantId = getLoginUser().getTenantId();
        return success(publicShareUserJoinService.getUserListInSelect(tenantId));
    }

    /**
     * 查询公摊用户关联列表
     */
    @GetMapping("/getUserPageForGJ")
    public TableDataInfo getUserPageForGJ(PublicShareUserJoin publicShareUserJoin)
    {
        startPage();
        publicShareUserJoin.setTenantId(getLoginUser().getTenantId());
        List<UsersXb> list = publicShareUserJoinService.getUserPageForGJ(publicShareUserJoin);
        return getDataTable(list);
    }


    @GetMapping("/getUserPageForGJProduct")
    public TableDataInfo getUserPageForGJProduct(PublicShareUserJoin publicShareUserJoin)
    {
        startPage();
        publicShareUserJoin.setTenantId(getLoginUser().getTenantId());
        List<Map<String, Object>> list = publicShareUserJoinService.getUserPageForGJProduct(publicShareUserJoin);

        return getDataTable(MapKeyUnderlineToCamelUtil.underlineToCamel(list));
    }

    /**
     * @Description: 根据用户id获取该用户下所有设备以及该用户下关联的公摊的其他所有用户的所有设备
     * @date: 2025/7/10 13:23
     * @author: zt
     */
    @GetMapping(value = "/getUserTerminalInPublicShareList")
    public AjaxResult getUserTerminalInPublicShareList(String userId){
        return success(publicShareUserJoinService.getUserTerminalInPublicShareList(userId));
    }

    /**
    * @Description: 办理用户离场
    * @date: 2025/7/10 15:41
    * @author: zt
    */
    @PostMapping(value = "/dealUserLeave")
    public AjaxResult dealUserLeave(@RequestBody UserTerminalsRequestVO requestVO){
       // return toAjax(publicShareUserJoinService.dealUserLeave(requestVO));
        return toAjax(billService.dealUserLeaveForinsertHistoryData(requestVO));
    }

}
